<template>
  <view class="page-register">
    <view class="back">
      <uni-icons type="left" color="#ffffff" size="24" @click="handleUserLogin"></uni-icons>
    </view>
    <view class="page-conent">
      <view class="info">
        <img src="~@/static/images/login/login-icon.png" alt="" srcset="" class="info-logo" />
        <text class="info-title">{{ $t('register.AB') }}</text>
      </view>
      <view class="form">
        <view class="register-form">
          <view class="form-txt">{{ $t('register.忘记密码') }}</view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/email.webp" alt="" srcset="" class="input-icon" />
            <input v-model="registerForm.email" class="input" type="text" :placeholder="$t('register.请输入您的邮箱')" maxlength="30" />
          </view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/verification-code.webp" alt="" srcset="" class="input-icon" />
            <input v-model="registerForm.code" class="input" type="text" :placeholder="$t('register.请输入验证码')" maxlength="20" />
            <view class="btn" @click="handleSendCode">{{ flag ? time : $t('register.发送') }}</view>
          </view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/password.webp" alt="" srcset="" class="input-icon" />
            <uni-easyinput v-model="registerForm.password" password type="password" class="input" :placeholder="$t('register.请输入新的密码')" maxlength="20" />
          </view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/confirm-password.webp" alt="" srcset="" class="input-icon" />
            <uni-easyinput v-model="registerForm.confirmPassword" password type="password" class="input" :placeholder="$t('register.请再次输入新的密码')" maxlength="20" />
          </view>
          <view class="action-btn">
            <button @click="handleRegister" class="login-btn">{{ $t('mine.提交') }}</button>
          </view>
          <!-- <view class="form-options">
            <view class="options-item" @click="handleUserLogin">{{ $t('register.去登录') }}</view>
          </view> -->
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import { accountCode, forgotPassword } from '@/api/login'

  export default {
    data() {
      return {
        codeUrl: '',
        globalConfig: getApp().globalData.config,
        registerForm: {
          email: '',
          code: '',
          password: '',
          confirmPassword: ''
        },
        time: null,
        flag: false
      }
    },
    methods: {
      // 用户登录
      handleUserLogin() {
        this.$tab.navigateTo(`/pages/login`)
      },
      // 注册方法
      async handleRegister() {
        if (this.registerForm.email === '') {
          this.$modal.msgError(this.$t('register.请输入您的账号'))
        } else if (this.registerForm.code === '') {
          this.$modal.msgError(this.$t('register.请输入验证码'))
        } else if (this.registerForm.password === '') {
          this.$modal.msgError(this.$t('register.请输入您的密码'))
        } else if (this.registerForm.confirmPassword === '') {
          this.$modal.msgError(this.$t('register.请再次输入您的密码'))
        } else if (this.registerForm.password !== this.registerForm.confirmPassword) {
          this.$modal.msgError(this.$t('register.两次输入的密码不一致'))
        } else {
          this.$modal.loading(this.$t('register.注册中，请耐心等待'))
          this.register()
        }
      },
      //发送验证码
      handleSendCode() {
        if (this.flag) return
        if (this.registerForm.email === '') {
          this.$modal.msgError(this.$t('register.请输入您的账号'))
        } else {
          this.$modal.loading(this.$t('register.发送中，请耐心等待'))
          accountCode({
            mobile: this.registerForm.email
          })
            .then(() => {
              this.$modal.closeLoading()
              this.$modal.msgSuccess(this.$t('register.验证码发送成功'))
              this.flag = true
              this.time = 60
              this.countdown()
            })
            .catch(() => {
              this.$modal.closeLoading()
              this.flag = false
            })
        }
      },
      countdown() {
        if (this.time > 0) {
          setTimeout(() => {
            this.countdown(this.time--)
          }, 1000)
        } else {
          this.flag = false
        }
      },
      // 忘记密码
      async register() {
        let postForm = {
          mobile: this.registerForm.email,
          code: this.registerForm.code,
          password: this.registerForm.password,
          password_repetition: this.registerForm.confirmPassword,
          group: 'h5'
        }
        forgotPassword(postForm).then((res) => {
          this.$modal.closeLoading()
          uni.showModal({
            title: this.$t('register.系统提示'),
            content: this.$t('register.密码修改成功') + '！',
            success: function (res) {
              if (res.confirm) {
                uni.redirectTo({ url: `/pages/login` })
              }
            }
          })
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page-register {
    width: 100%;
    height: 100vh;
    background: url(~@/static/images/login/login-bg-old.png) 0% 0% / 100% 100vh no-repeat;
    overflow: hidden;
    .page-conent {
      height: 100%;
      overflow-y: auto;
    }
    .back {
      position: fixed;
      width: 100vw;
      left: 0;
      padding-left: 16rpx;
      padding-top: 16rpx;
    }
    .info {
      padding-top: 140rpx;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .info-logo {
        width: 160rpx;
        height: 160rpx;
      }
      .info-title {
        font-size: 60rpx;
        margin-top: 20rpx;
        margin-bottom: 50rpx;
      }
    }
    .form {
      width: 100vw;
      padding: 0 30rpx;
      margin-top: 20rpx;
      margin-bottom: 40rpx;
      .register-form {
        width: 100%;
        min-height: 50vh;
        border-radius: 10px;
        padding: 30px;
        box-sizing: border-box;
        background: hsla(0, 0%, 100%, 0.8);
        .form-txt {
          font-size: 52rpx;
          text-align: center;
        }
        .input-item {
          margin: 20px auto;
          height: 45px;
          border-bottom: 1rpx solid #6083d6;
          display: flex;
          align-items: center;
          .input-icon {
            width: 44rpx;
            height: 44rpx;
          }
          .input {
            flex: 1;
            overflow: hidden;
            font-size: 14px;
            line-height: 20px;
            text-align: left;
            padding-left: 15px;
            ::v-deep.uni-easyinput__content {
              border: none !important;
              background-color: transparent !important;
            }
          }
          .btn {
            height: 60rpx;
            line-height: 60rpx;
            padding: 0 30rpx;
            background-color: #5cb1b4;
            border-radius: 10rpx;
            color: #fff;
            font-size: 28rpx;
          }
        }

        .login-btn {
          width: 70vw;
          // background: linear-gradient(90deg, #6284d6, #2d63e3);
          background: linear-gradient(90deg, #31d6e0, #5599e0);
          margin: 130rpx auto 40rpx;
          height: 80rpx;
          border-radius: 20px;
          font-size: 32rpx;
          color: #fff;
          line-height: 80rpx;
          text-align: center;
        }
        .login-code {
          height: 38px;
          // float: right;
          .login-code-img {
            height: 38px;
            // position: absolute;
            margin-left: 10px;
            width: 200rpx;
          }
        }
        .form-options {
          font-size: 24rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
</style>
